package com.dqyt.khjc.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.dqyt.app.bean.Maintenance;
import com.dqyt.app.bean.Report;
import com.dqyt.app.bean.Sample;
import com.dqyt.app.bean.WorkBean;
import com.dqyt.app.config.RepairType;
import com.dqyt.app.mongo.MaintenanceDao;
import com.dqyt.app.mongo.ReportDao;
import com.dqyt.app.mongo.SampleDao;
import com.dqyt.common.check.CheckTool;
import com.dqyt.common.constant.AuthorityID;
import com.dqyt.common.constant.Opt;
import com.dqyt.common.utils.BHUtil;
import com.dqyt.common.utils.StringUtil;
import com.dqyt.common.utils.UserInfoUtil;
import com.dqyt.khjc.bean.*;
import com.dqyt.khjc.mapper.HistoryMapper;
import com.dqyt.khjc.mapper.QrCodeMapper;
import com.dqyt.khjc.mapper.WorkMapper;
import com.dqyt.khjc.swicth.SwitchConfig;
import com.dqyt.khjc.utils.IpUtil;
import com.dqyt.khjc.utils.TimeUtil;
import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@Service
public class MaintenanceService {
    @Autowired
    private SampleDao mSampleDao;

    @Autowired
    private ReportDao reportDao;

    @Autowired
    private MaintenanceDao maintenanceDao;

    @Autowired
    private QrCodeMapper qrCodeMapper;


    @Autowired
    private HistoryMapper historyMapper;

    @Autowired
    private WorkMapper workMapper;
    /**
     * 添加维修记录
     * @param request
     * @param maintenance
     * @return
     */
    public ResultMsg saveMaintenance(HttpServletRequest request, String maintenance){
        ResultMsg ret = new ResultMsg(); // 声明一个返回值
        try {
            ret = UserInfoUtil.isAuthori_Web(request);
            // 如果有错误信息
            if (ret != null) {
                return ret;
            }
            ret = new ResultMsg(); // 声明一个返回值
            // 权限检查
            // 首先取得用户信息
            User user = UserInfoUtil.getUser_Web(request);
            Maintenance maintenance1 = JSON.parseObject(maintenance, Maintenance.class);
            //查询到
            Sample oldDataQp = mSampleDao.getSampleById(maintenance1.bsbh);
            //查询报告
            Report mreport1 = reportDao.getReportBybsbh(maintenance1.bsbh);
            //如果报告已经生成了 不不改变样品的状态了
            if(null !=mreport1){

            }else{
                //更新状态
                //oldDataQp.sampleStatue = RepairType.TYPE_WXZ;
                mSampleDao.save(oldDataQp);
            }
            //如果背板 更换部件为空
            if("".equals(maintenance1.bbGhbjbh)){
                maintenance1.bbYbjbh="";
            }
            //如果减压器 跟换部件为空
            if("".equals(maintenance1.jyqGhbjbh)){
                maintenance1.jyqYbjbh="";
            }
            //如果气瓶跟换部件为空
            if("".equals(maintenance1.qpGhbjbh)){
                maintenance1.qpYbjbh ="";
            }
            // 数据检查
            if (StringUtil.isEmpty(maintenance1.bsbh) || StringUtil.isEmpty(maintenance1.erweima)) {
                ret.setCode(ResultMsg.ERROR);
                ret.setMsg("数据错误！");
                return ret;
            }


            //设置 自己的id 到修理员
            maintenance1.wxy=user.getUserName();
            //维修员id
            maintenance1.wxyId = user.getUserId();
            //设置核验员未签字
            maintenance1.hyzt = 0;
            long time = TimeUtil.getCurrentTimeMillis();
            maintenance1.setAddUser(user.getUserId() + "");
            maintenance1.setAddTime(time);
            maintenance1.setUpdateUser(user.getUserId() + "");
            maintenance1.setUpdateTime(time);
            maintenance1.setSysStatus(BaseBean.SYS_STATUS_OK);
            //任务编号
            maintenance1.rwdbh = oldDataQp.rwdbh;
            //保存数据
            maintenanceDao.save(maintenance1);
            ret.setCode(ResultMsg.SUCESS);
            ret.setMsg("保存成功");

            //如果操作日志的开关已经打开
            if(SwitchConfig.isOpen(SwitchConfig.ID_CZRZ)){
                //添维修记录
                String ip = IpUtil.getIpAddr(request);
                HistoryBean historyBean = new HistoryBean();
                historyBean.setMessage("[" + user.getUserName() + "] 添加了维修记录，维修设备编号为： [" + maintenance1.sbbh + "] ");
                historyBean.setType(Opt.SBWX_XZSBWX_TJWXJL);
                historyBean.setOpUserId(user.getUserId());
                historyBean.setIp(ip);
                historyBean.setBopId(String.valueOf(maintenance1.sbbh));
                historyBean.setAddTime(TimeUtil.currentTimeMillis());
                historyBean.setAddUser(String.valueOf(user.getUserId()));
                historyBean.setUpdateTime(TimeUtil.currentTimeMillis());
                historyBean.setUpdateUser(String.valueOf(user.getUserId()));
                //插入数据库
                int ress = historyMapper.addHistory(historyBean);
            }

        } catch (Exception e) {
            ret.setCode(ResultMsg.ERROR);
            ret.setMsg("未知的错误，请稍候再试！");
            e.printStackTrace();
        }
        return ret;
    }


    /**
     * 无实物添加维修记录
     * @param request
     * @param maintenance
     * @return
     */
    public ResultMsg saveMaintenanceWsw(HttpServletRequest request, String maintenance){
        ResultMsg ret = new ResultMsg(); // 声明一个返回值
        try {
            ret = UserInfoUtil.isAuthori_Web(request);
            // 如果有错误信息
            if (ret != null) {
                return ret;
            }
            ret = new ResultMsg(); // 声明一个返回值
            // 权限检查
            // 首先取得用户信息
            User user = UserInfoUtil.getUser_Web(request);

            if(null ==user){
                ret.setCode(ResultMsg.ERROR);
                ret.setMsg("登录过期");
                return ret;
            }

            Maintenance maintenance1 = JSON.parseObject(maintenance, Maintenance.class);
            //查询work表
            WorkBean workBean =   workMapper.getWorkListByBsbh(maintenance1.bsbh);
            if(null == workBean){
                ret.setCode(ResultMsg.ERROR);
                ret.setMsg("在任务分配列表中没有找到标识编号为"+maintenance1.bsbh+"的任务");
                return ret;
            }

            Sample sample= mSampleDao.getPcSampleByBsbh(maintenance1.bsbh);
            if(null == sample){
                ret.setCode(ResultMsg.ERROR);
                ret.setMsg("请登记标识编号为"+maintenance1.bsbh+"的样品信息");
                return ret;
            }


            maintenance1.sbbh = sample.sbbh;
            maintenance1.rwdbh = workBean.getRwdbh();
            maintenance1.erweima =sample.erweima;

            if(maintenance1.type != sample.type){
                ret.setCode(ResultMsg.ERROR);
                ret.setMsg("标识编号和维修的类型不匹配");
                return ret;
            }
            maintenance1.type = sample.type;
            if(sample.type == 1){
                maintenance1.wtfId = sample.respirator.wtfId;
                maintenance1.wtf = sample.respirator.wtf;
                maintenance1.scjyrq =sample.respirator.scjyrq;
            }else{
                maintenance1.wtfId = sample.cylinders.wtfId;
                maintenance1.wtf = sample.cylinders.wtf;
                maintenance1.scjyrq = sample.cylinders.scjyrq;
            }

            maintenance1.wtfIdChi = sample.wtfIdChi;
            maintenance1.wtfChi = sample.wtfChi;

            maintenance1.sjrId = maintenance1.sjrId;
            maintenance1.sjr = maintenance1.sjr;
            maintenance1.wxy = user.getUserName();
            maintenance1.wxyId = user.getUserId();
            maintenance1.hyzt = 0;
            maintenanceDao.save(maintenance1);
            ret.setCode(ResultMsg.SUCESS);
            ret.setMsg("保存成功");
            //如果操作日志的开关已经打开
            if(SwitchConfig.isOpen(SwitchConfig.ID_CZRZ)){
                //添维修记录
                String ip = IpUtil.getIpAddr(request);
                HistoryBean historyBean = new HistoryBean();
                historyBean.setMessage("[" + user.getUserName() + "] 添加了维修记录，维修设备编号为： [" + maintenance1.sbbh + "] ");
                historyBean.setType(Opt.SBWX_XZSBWX_TJWXJL);
                historyBean.setOpUserId(user.getUserId());
                historyBean.setIp(ip);
                historyBean.setBopId(String.valueOf(maintenance1.sbbh));
                historyBean.setAddTime(TimeUtil.currentTimeMillis());
                historyBean.setAddUser(String.valueOf(user.getUserId()));
                historyBean.setUpdateTime(TimeUtil.currentTimeMillis());
                historyBean.setUpdateUser(String.valueOf(user.getUserId()));
                //插入数据库
                int ress = historyMapper.addHistory(historyBean);
            }
            System.out.println(maintenance1);
        } catch (Exception e) {
            ret.setCode(ResultMsg.ERROR);
            ret.setMsg("未知的错误，请稍候再试！");
            e.printStackTrace();
        }
        return ret;
    }


    /**
     * 修改维修记录
     * @param request
     * @param maintenance
     * @return
     */
    public ResultMsg updateMaintenance(HttpServletRequest request, String maintenance){
        ResultMsg ret = new ResultMsg(); // 声明一个返回值
        try {
            ret = UserInfoUtil.isAuthori_Web(request);
            // 如果有错误信息
            if (ret != null) {
                return ret;
            }
            ret = new ResultMsg(); // 声明一个返回值
            // 权限检查
            // 首先取得用户信息
            User user = UserInfoUtil.getUser_Web(request);
            // 用户权限检查 是否可以管理待审核申请
//            if (!CheckTool.checkAuthority(user, AuthorityID.SBWX_XZSBWX)) {
//                ret.setCode(ResultMsg.ERROR);
//                ret.setMsg("你没有对应的操作权限");
//                return ret;
//            }

            Maintenance maintenance1 = JSON.parseObject(maintenance, Maintenance.class);
            // 数据检查
            if (StringUtil.isEmpty(maintenance1.bsbh) || StringUtil.isEmpty(maintenance1.erweima)) {
                ret.setCode(ResultMsg.ERROR);
                ret.setMsg("数据错误！");
                return ret;
            }
            long time = TimeUtil.getCurrentTimeMillis();
            maintenance1.setUpdateUser(user.getUserId() + "");
            maintenance1.setUpdateTime(time);
            //保存数据
            maintenanceDao.save(maintenance1);
            ret.setCode(ResultMsg.SUCESS);
            ret.setMsg("保存成功");


            //如果操作日志的开关已经打开
            if(SwitchConfig.isOpen(SwitchConfig.ID_CZRZ)){
                //添维修记录
                String ip = IpUtil.getIpAddr(request);
                HistoryBean historyBean = new HistoryBean();
                historyBean.setMessage("[" + user.getUserName() + "] 修改了维修记录，维修设备编号为： [" + maintenance1.sbbh + "] ");
                historyBean.setType(Opt.SBWX_XZSBWX_XGWXJL);
                historyBean.setOpUserId(user.getUserId());
                historyBean.setIp(ip);
                historyBean.setBopId(String.valueOf(maintenance1.sbbh));
                historyBean.setAddTime(TimeUtil.currentTimeMillis());
                historyBean.setAddUser(String.valueOf(user.getUserId()));
                historyBean.setUpdateTime(TimeUtil.currentTimeMillis());
                historyBean.setUpdateUser(String.valueOf(user.getUserId()));
                //插入数据库
                int ress = historyMapper.addHistory(historyBean);
            }



        } catch (Exception e) {
            ret.setCode(ResultMsg.ERROR);
            ret.setMsg("未知的错误，请稍候再试！");
            e.printStackTrace();
        }
        return ret;
    }


    /**
     * 分页查询维修列表
     * @param pageNum  页数
     * @param pageSize  每页条数
     * @param bsbh   标识编号
     * @param erweima  二维码
     * @param type  样品类型  1：呼吸器  2: 复合气瓶
     * @param wtfId  委托方id
     *  @param jyrqNian  按年收
     *  @param jyrqYue  按年 月收
     * @return
     */
    public ResultMsg getMainteListPage(HttpServletRequest request,Integer pageNum,Integer pageSize,String bsbh,String erweima,
                                       int type,int wtfId,int wtfIdChi,String jyrqNian,String jyrqYue,String rwdbh){
        List<Sample> sampleList = new ArrayList<Sample>();
        ResultMsg ret = new ResultMsg(); // 声明一个返回值
        try {
            // 检查用户是否登录Token是否正确
            ret = UserInfoUtil.isAuthori_Web(request);
            // 如果有错误信息
            if (ret != null) {
                return ret;
            }
            ret = new ResultMsg(); // 声明一个返回值
            // 权限检查
            // 首先取得用户信息
            User user = UserInfoUtil.getUser_Web(request);

            if (pageNum != null && pageSize != null) {
                pageNum = (pageNum - 1) * pageSize;
            }

            //分页查询数据
            List<Maintenance>  maintenances =  maintenanceDao.getMainteListPage( pageNum, pageSize, bsbh, erweima, type, wtfId,wtfIdChi, jyrqNian,jyrqYue,rwdbh);
            //查询总条数
            long  total = maintenanceDao.getMainteListTotle(bsbh, erweima, type, wtfId,wtfIdChi, jyrqNian,jyrqYue,rwdbh);
            RespPageBean bean = new RespPageBean();
            bean.setData(maintenances);
            bean.setTotal(total);
            ret.setCode(ResultMsg.SUCESS);
            ret.setData(bean);
        }catch (Exception e) {
            ret.setCode(ResultMsg.ERROR);
            ret.setMsg("未知的错误，请稍候再试！");
            e.printStackTrace();
        }
        return ret;
    };


    /**
     * 分页查询维修列表  自己的维修列表
     * @param pageNum  页数
     * @param pageSize  每页条数
     * @param bsbh   标识编号
     * @param erweima  二维码
     * @param type  样品类型  1：呼吸器  2: 复合气瓶
     * @param wtfId  委托方id
     *  @param jyrqNian  按年收
     *  @param jyrqYue  按年 月收
     * @return
     */
    public ResultMsg getMainteListPageMe(HttpServletRequest request,Integer pageNum,Integer pageSize,String bsbh,String erweima,
                                       int type,int wtfId,int wtfIdChi,String jyrqNian,String jyrqYue,String rwdbh){
        List<Sample> sampleList = new ArrayList<Sample>();
        ResultMsg ret = new ResultMsg(); // 声明一个返回值
        try {
            // 检查用户是否登录Token是否正确
            ret = UserInfoUtil.isAuthori_Web(request);
            // 如果有错误信息
            if (ret != null) {
                return ret;
            }
            ret = new ResultMsg(); // 声明一个返回值
            // 权限检查
            // 首先取得用户信息
            User user = UserInfoUtil.getUser_Web(request);
            if(null ==user){
                ret.setCode(ResultMsg.ERROR);
                ret.setMsg("登录过期");
                return ret;
            }
            if (pageNum != null && pageSize != null) {
                pageNum = (pageNum - 1) * pageSize;
            }




            //分页查询数据
            List<Maintenance>  maintenances =  maintenanceDao.getMainteListPageMe( pageNum, pageSize, bsbh, erweima, type, wtfId,wtfIdChi, jyrqNian,jyrqYue,rwdbh,user.getUserId());
            //查询总条数
            long  total = maintenanceDao.getMainteListTotleMe(bsbh, erweima, type, wtfId,wtfIdChi, jyrqNian,jyrqYue,rwdbh,user.getUserId());
            RespPageBean bean = new RespPageBean();
            bean.setData(maintenances);
            bean.setTotal(total);
            ret.setCode(ResultMsg.SUCESS);
            ret.setData(bean);
        }catch (Exception e) {
            ret.setCode(ResultMsg.ERROR);
            ret.setMsg("未知的错误，请稍候再试！");
            e.printStackTrace();
        }
        return ret;
    };



    /**
     * 分页查询 自己待审核的维修列表
     * @param pageNum  页数
     * @param pageSize  每页条数
     * @param bsbh   标识编号
     * @param erweima  二维码
     * @param type  样品类型  1：呼吸器  2: 复合气瓶
     * @param wtfId  委托方id
     *  @param jyrqNian  按年收
     *  @param jyrqYue  按年 月收
     * @return
     */
    public ResultMsg getMyMainteListPage(HttpServletRequest request,Integer pageNum,Integer pageSize,String bsbh,String erweima,int type,int wtfId,String jyrqNian,String jyrqYue){
        List<Sample> sampleList = new ArrayList<Sample>();
        ResultMsg ret = new ResultMsg(); // 声明一个返回值
        try {
            // 检查用户是否登录Token是否正确
            ret = UserInfoUtil.isAuthori_Web(request);
            // 如果有错误信息
            if (ret != null) {
                return ret;
            }
            ret = new ResultMsg(); // 声明一个返回值
            // 权限检查
            // 首先取得用户信息
            User user = UserInfoUtil.getUser_Web(request);
            // 用户权限检查 是否有客户信息一览的权限
//            if (!CheckTool.checkAuthority(user, AuthorityID.SBWX_WXLB)) {
//                ret.setCode(ResultMsg.ERROR);
//                ret.setMsg("你没有对应的操作权限");
//                return ret;
//            }
            if (pageNum != null && pageSize != null) {
                pageNum = (pageNum - 1) * pageSize;
            }

            long hyyId = user.getUserId();
            //分页查询数据
            List<Maintenance>  maintenances =  maintenanceDao.getMyMainteListPage( pageNum, pageSize, bsbh, erweima, type, wtfId, jyrqNian,jyrqYue,hyyId);
            //查询总条数
            long  total = maintenanceDao.getMyMainteListTotle(bsbh, erweima, type, wtfId, jyrqNian,jyrqYue,hyyId);
            RespPageBean bean = new RespPageBean();
            bean.setData(maintenances);
            bean.setTotal(total);
            ret.setCode(ResultMsg.SUCESS);
            ret.setData(bean);
        }catch (Exception e) {
            ret.setCode(ResultMsg.ERROR);
            ret.setMsg("未知的错误，请稍候再试！");
            e.printStackTrace();
        }
        return ret;
    };

    /**
     * 自己待审核的维修条数
     * @param request
     * @return
     */
    public ResultMsg getDshWxTot(HttpServletRequest request){
        ResultMsg ret = new ResultMsg(); // 声明一个返回值
        try {
            // 检查用户是否登录Token是否正确
            ret = UserInfoUtil.isAuthori_Web(request);
            // 如果有错误信息
            if (ret != null) {
                return ret;
            }
            ret = new ResultMsg(); // 声明一个返回值
            // 权限检查
            // 首先取得用户信息
            User user = UserInfoUtil.getUser_Web(request);
            //查询总条数
            long  total = maintenanceDao.getMyMainteListTotle2(user.getUserId());
            ret.setCode(ResultMsg.SUCESS);
            ret.setData(total);
        }catch (Exception e) {
            ret.setCode(ResultMsg.ERROR);
            ret.setMsg("未知的错误，请稍候再试！");
            e.printStackTrace();
        }
        return ret;
    }


    /**
     * 查询维修记录 根据标识编号
     * @param request
     * @param bsbh
     * @return
     */
    public ResultMsg getMainteBybsbh(HttpServletRequest request,String bsbh){
        List<Sample> sampleList = new ArrayList<Sample>();
        ResultMsg ret = new ResultMsg(); // 声明一个返回值
        try {
            // 检查用户是否登录Token是否正确
            ret = UserInfoUtil.isAuthori_Web(request);
            // 如果有错误信息
            if (ret != null) {
                return ret;
            }
            ret = new ResultMsg(); // 声明一个返回值
            // 权限检查
            // 首先取得用户信息
            User user = UserInfoUtil.getUser_Web(request);
            // 用户权限检查 是否有客户信息一览的权限
//            if (!CheckTool.checkAuthority(user, AuthorityID.SBWX_WXLB)) {
//                ret.setCode(ResultMsg.ERROR);
//                ret.setMsg("你没有对应的操作权限");
//                return ret;
//            }
            Maintenance maintenance= maintenanceDao.getMainteBybsbh(bsbh);
            ret.setCode(ResultMsg.SUCESS);
            ret.setMsg("成功");
            ret.setData(maintenance);
        }catch (Exception e) {
            ret.setCode(ResultMsg.ERROR);
            ret.setMsg("未知的错误，请稍候再试！");
            e.printStackTrace();
        }
        return ret;
    }



    /**
     * 审核  维修
     * @param request
     * @param maintenance
     * @return
     */
    public ResultMsg examineMaintenance(HttpServletRequest request, String maintenance){
        ResultMsg ret = new ResultMsg(); // 声明一个返回值
        try {
            ret = UserInfoUtil.isAuthori_Web(request);
            // 如果有错误信息
            if (ret != null) {
                return ret;
            }
            ret = new ResultMsg(); // 声明一个返回值
            // 权限检查
            // 首先取得用户信息
            User user = UserInfoUtil.getUser_Web(request);

            Maintenance maintenance1 = JSON.parseObject(maintenance, Maintenance.class);

            //如果背板 更换部件为空
            if("".equals(maintenance1.bbGhbjbh)){
                maintenance1.bbYbjbh="";
            }

            //如果减压器 跟换部件为空
            if("".equals(maintenance1.jyqGhbjbh)){
                maintenance1.jyqYbjbh="";
            }

            //如果气瓶跟换部件为空
            if("".equals(maintenance1.qpGhbjbh)){
                maintenance1.qpYbjbh ="";
            }

            // 数据检查
            if (StringUtil.isEmpty(maintenance1.bsbh) || StringUtil.isEmpty(maintenance1.erweima)) {
                ret.setCode(ResultMsg.ERROR);
                ret.setMsg("数据异常请刷新重试！");
                return ret;
            }


            //如果是维修面罩
            if(maintenance1.type == 1){
                //查询到
                Sample oldData = mSampleDao.getSampleById(maintenance1.bsbh);
                //更新状态
                //oldData.sampleStatue = RepairType.TYPE_WXZ;
                //先保存一下状态
                mSampleDao.save(oldData);

                //如果背板原部件编号 和 更换背板原部件编号都不为空
                if(!"".equals(maintenance1.bbYbjbh) && !"".equals(maintenance1.bbGhbjbh)){
                    //将oldData  拷贝到moldSample
                    Sample  moldSample  =  JSONObject.parseObject(JSONObject.toJSONBytes(oldData), Sample.class);
                    moldSample.bh = oldData.bsbh+"_old";
                    //将表示编号后面加上_old
                    moldSample.bsbh = oldData.bsbh+"_old";
                    moldSample.bsbhHrader = oldData.bsbh+"_old";
                    moldSample.respirator.bsbh = oldData.bsbh+"_old";
                    //把他设置为
                    moldSample.setSysStatus(0);

                    //转了背板之后的对象------------------------start---------------
                    //将oldData  拷贝到moldSample
                    Sample  newWxSample  =  JSONObject.parseObject(JSONObject.toJSONBytes(oldData), Sample.class);
                    //设备编号
                    newWxSample.sbbh = maintenance1.bbGhbjbh;
                    //设备编号
                    newWxSample.respirator.sbbh= maintenance1.bbGhbjbh;
                    //设置为新品
                    //newWxSample.respirator.sfxp =1;

                    //还需要将这个面罩的整机气密性 和 报警声强清除
                    newWxSample.respirator.zjqmx ="";
                    newWxSample.respirator.bjsq ="";


                    //如果报告已经被生成出来了  但是背板被替换了 那就新设置报告的状态是新品
                    Report report = reportDao.getReportBybsbhOrsb(newWxSample.bsbh,newWxSample.sbbh,Sample.YP_TYPE_HXQ);
                    if(null !=report){
                        //设置新品
                        report.respirator.nr_ypdj = "新品";
                        reportDao.save(report);
                    }

                    //新生成二维码
                    //在根据旧的数据吧二维码查询出来
                    QrCodeBean qrCodeBean =  qrCodeMapper.getQrCodeBySbbh(maintenance1.bbGhbjbh);
                    if(null !=qrCodeBean){
                        newWxSample.erweima =qrCodeBean.getErweima();
                        newWxSample.respirator.erweima = qrCodeBean.getErweima();
                    }else{
                        qrCodeBean = new QrCodeBean();
                        String merwema = BHUtil.getErWeiMa(1,maintenance1.bbGhbjbh);
                        //将二维码放到数据库
                        qrCodeBean.setSbbh(maintenance1.bbGhbjbh);
                        qrCodeBean.setErweima(merwema);
                        long nowTime = TimeUtil.getCurrentTimeMillis();
                        qrCodeBean.setAddTime(nowTime);
                        qrCodeBean.setAddUser(user.getUserId()+"");
                        qrCodeBean.setUpdateTime(nowTime);
                        qrCodeBean.setUpdateUser(user.getUserId()+"");
                        qrCodeMapper.saveQrCode(qrCodeBean);
                        newWxSample.erweima = merwema;
                        newWxSample.respirator.erweima = merwema;
                    }

                    //将老的这个 匹配气瓶信息清空
                    moldSample.respirator.qpbsh ="";
                    moldSample.respirator.qpbh ="";
                    moldSample.respirator.qpsccsId=0;
                    moldSample.respirator.qpsccs = "";
                    moldSample.respirator.qpscrq = "";
                    moldSample.respirator.qpjyyxq ="";

                    newWxSample.setAddTime(TimeUtil.currentTimeMillis());
                    //修改任务单里面的 对应数据的 设备编号
                    workMapper.upWorkListByBsbhOrRwdbh(oldData.bsbh,oldData.rwdbh,newWxSample.sbbh);
                    long res =  mSampleDao.delete(oldData);
                    if(res>0){
                        mSampleDao.save(moldSample);
                        mSampleDao.save(newWxSample);
                    }
                    //转了背板之后的对象----------------end-----------------------
                }

                //如果是减压器原部件编号 和更换 减压器部件编号不为空
                if(!"".equals(maintenance1.jyqYbjbh) && !"".equals(maintenance1.jyqGhbjbh)){
                    //将oldData  拷贝到moldSample
                    Sample  moldSample  =  JSONObject.parseObject(JSONObject.toJSONBytes(oldData), Sample.class);
                    moldSample.bh = oldData.bsbh+"_old";
                    //将表示编号后面加上_old
                    moldSample.bsbh = oldData.bsbh+"_old";
                    moldSample.bsbhHrader = oldData.bsbh+"_old";
                    moldSample.respirator.bsbh = oldData.bsbh+"_old";
                    //把他设置为
                    moldSample.setSysStatus(0);

                    //转了减压器之后的对象------------------------start---------------
                    //将oldData  拷贝到moldSample
                    Sample  newWxSample  =  JSONObject.parseObject(JSONObject.toJSONBytes(oldData), Sample.class);
                    //设备编号
                    newWxSample.sbbh = maintenance1.jyqGhbjbh;
                    //设备编号
                    newWxSample.respirator.sbbh= maintenance1.jyqGhbjbh;
                    newWxSample.respirator.ccbh = newWxSample.respirator.sbbh;

                    //新生成二维码
                    //在根据旧的数据吧二维码查询出来
                    QrCodeBean qrCodeBean =  qrCodeMapper.getQrCodeBySbbh(maintenance1.jyqGhbjbh);
                    if(null !=qrCodeBean){
                        newWxSample.erweima =qrCodeBean.getErweima();
                        newWxSample.respirator.erweima = qrCodeBean.getErweima();
                    }else{
                        qrCodeBean = new QrCodeBean();
                        String merwema = BHUtil.getErWeiMa(1,maintenance1.jyqGhbjbh);
                        //将二维码放到数据库
                        qrCodeBean.setSbbh(maintenance1.jyqGhbjbh);
                        qrCodeBean.setErweima(merwema);
                        long nowTime = TimeUtil.getCurrentTimeMillis();
                        qrCodeBean.setAddTime(nowTime);
                        qrCodeBean.setAddUser(user.getUserId()+"");
                        qrCodeBean.setUpdateTime(nowTime);
                        qrCodeBean.setUpdateUser(user.getUserId()+"");
                        qrCodeMapper.saveQrCode(qrCodeBean);
                        newWxSample.erweima = merwema;
                        newWxSample.respirator.erweima = merwema;
                    }
                    long res =  mSampleDao.delete(oldData);
                    //将老的这个 匹配气瓶信息清空
                    moldSample.respirator.qpbsh ="";
                    moldSample.respirator.qpbh ="";
                    moldSample.respirator.qpsccsId=0;
                    moldSample.respirator.qpsccs = "";
                    moldSample.respirator.qpscrq = "";
                    moldSample.respirator.qpjyyxq ="";
                    newWxSample.setAddTime(TimeUtil.currentTimeMillis());

                    //修改任务单里面的 对应数据的 设备编号
                    workMapper.upWorkListByBsbhOrRwdbh(newWxSample.bsbh,oldData.rwdbh,newWxSample.sbbh);
                    if(res>0){
                        mSampleDao.save(moldSample);
                        mSampleDao.save(newWxSample);
                    }
                    //转了减压器之后的对象----------------end-----------------------
                }

            }else{   /// 如果是换的气瓶
                //查询到
                Sample oldDataQp = mSampleDao.getSampleById(maintenance1.bsbh);
                //更新状态
                //oldDataQp.sampleStatue = RepairType.TYPE_WXZ;
                //先保存一下状态
                mSampleDao.save(oldDataQp);
                if(!"".equals(maintenance1.qpYbjbh) && !"".equals(maintenance1.qpGhbjbh)){

                    //将oldData  拷贝到moldSample
                    Sample  moldSample  =  JSONObject.parseObject(JSONObject.toJSONBytes(oldDataQp), Sample.class);
                    moldSample.bh = oldDataQp.bsbh+"_old";
                    //将表示编号后面加上_old
                    moldSample.bsbh = oldDataQp.bsbh+"_old";
                    moldSample.bsbhHrader = oldDataQp.bsbh+"_old";
                    moldSample.cylinders.bsbh = oldDataQp.bsbh+"_old";
                    //把他设置为
                    moldSample.setSysStatus(0);


                    //换了气瓶之后的对象------------------------start---------------
                    //将oldData  拷贝到moldSample
                    Sample  newWxSample  =  JSONObject.parseObject(JSONObject.toJSONBytes(oldDataQp), Sample.class);
                    //设备编号
                    newWxSample.sbbh = maintenance1.qpGhbjbh;
                    //设备编号
                    newWxSample.cylinders.sbbh= maintenance1.qpGhbjbh;
                    //新生成二维码
                    //在根据旧的数据吧二维码查询出来
                    QrCodeBean qrCodeBean =  qrCodeMapper.getQrCodeBySbbh(maintenance1.qpGhbjbh);
                    if(null !=qrCodeBean){
                        newWxSample.erweima =qrCodeBean.getErweima();
                        newWxSample.cylinders.erweima = qrCodeBean.getErweima();
                    }else{
                        qrCodeBean = new QrCodeBean();
                        String merwema = BHUtil.getErWeiMa(1,maintenance1.qpGhbjbh);
                        //将二维码放到数据库
                        qrCodeBean.setSbbh(maintenance1.qpGhbjbh);
                        qrCodeBean.setErweima(merwema);
                        long nowTime = TimeUtil.getCurrentTimeMillis();
                        qrCodeBean.setAddTime(nowTime);
                        qrCodeBean.setAddUser(user.getUserId()+"");
                        qrCodeBean.setUpdateTime(nowTime);
                        qrCodeBean.setUpdateUser(user.getUserId()+"");
                        qrCodeMapper.saveQrCode(qrCodeBean);
                        newWxSample.erweima = merwema;
                        newWxSample.cylinders.erweima = merwema;
                    }
                    long res =  mSampleDao.delete(oldDataQp);
                    newWxSample.setAddTime(TimeUtil.currentTimeMillis());

                    //修改任务单里面的 对应数据的 设备编号
                    workMapper.upWorkListByBsbhOrRwdbh(newWxSample.bsbh,moldSample.rwdbh,newWxSample.sbbh);

                    if(res>0){
                        mSampleDao.save(moldSample);
                        mSampleDao.save(newWxSample);
                    }
                }
            }

            //设置 自己的id 到修理员
            maintenance1.hyy=user.getUserName();
            //维修员id
            maintenance1.hyyId = user.getUserId();
            //设置核验员未签字
            maintenance1.hyzt = 1;
            long time = TimeUtil.getCurrentTimeMillis();
            maintenance1.setUpdateUser(user.getUserId() + "");
            maintenance1.setUpdateTime(time);
            maintenance1.setSysStatus(BaseBean.SYS_STATUS_OK);
            //保存数据
            maintenanceDao.save(maintenance1);
            ret.setCode(ResultMsg.SUCESS);
            ret.setMsg("保存成功");


            //如果操作日志的开关已经打开
            if(SwitchConfig.isOpen(SwitchConfig.ID_CZRZ)){
                //添维修记录
                String ip = IpUtil.getIpAddr(request);
                HistoryBean historyBean = new HistoryBean();
                historyBean.setMessage("[" + user.getUserName() + "] 审批了：设备编号： [" + maintenance1.sbbh + "] 维修记录");
                historyBean.setType(Opt.SBWX_XZSBWX_SPWX);
                historyBean.setOpUserId(user.getUserId());
                historyBean.setIp(ip);
                historyBean.setBopId(String.valueOf(maintenance1.sbbh));
                historyBean.setAddTime(TimeUtil.currentTimeMillis());
                historyBean.setAddUser(String.valueOf(user.getUserId()));
                historyBean.setUpdateTime(TimeUtil.currentTimeMillis());
                historyBean.setUpdateUser(String.valueOf(user.getUserId()));
                //插入数据库
                int ress = historyMapper.addHistory(historyBean);
            }
        } catch (Exception e) {
            ret.setCode(ResultMsg.ERROR);
            ret.setMsg("未知的错误，请稍候再试！");
            e.printStackTrace();
        }
        return ret;
    }




    /**
     * 删除维修
     * @param request
     * @param /bh
     * @return
     */
    public ResultMsg deleteMainte(HttpServletRequest request,String _id){
        List<Sample> sampleList = new ArrayList<Sample>();
        ResultMsg ret = new ResultMsg(); // 声明一个返回值
        try {
            // 检查用户是否登录Token是否正确
            ret = UserInfoUtil.isAuthori_Web(request);
            // 如果有错误信息
            if (ret != null) {
                return ret;
            }
            ret = new ResultMsg(); // 声明一个返回值
            // 权限检查
            // 首先取得用户信息
            User user = UserInfoUtil.getUser_Web(request);

            long res= maintenanceDao.deleteMainte(_id);
            if(res>0) {
                ret.setCode(ResultMsg.SUCESS);
                ret.setMsg("操作成功");
                //添加操作记录
                // 先取得用户ip
                String ip = IpUtil.getIpAddr(request);
                HistoryBean historyBean = new HistoryBean();
                historyBean.setMessage("[" + user.getUserName() + "] 删除了id为： [" + _id + "] 的维修记录");
                historyBean.setType(Opt.SBWX_WXLB_SC);
                historyBean.setOpUserId(user.getUserId());
                historyBean.setIp(ip);
                historyBean.setBopId(_id);
                historyBean.setAddTime(TimeUtil.currentTimeMillis());
                historyBean.setAddUser(String.valueOf(user.getUserId()));
                historyBean.setUpdateTime(TimeUtil.currentTimeMillis());
                historyBean.setUpdateUser(String.valueOf(user.getUserId()));
                //插入数据库
                int ress = historyMapper.addHistory(historyBean);
            }else{
                ret.setCode(ResultMsg.ERROR);
                ret.setMsg("数据异常，请稍候在试");
            }

        }catch (Exception e) {
            ret.setCode(ResultMsg.ERROR);
            ret.setMsg("未知的错误，请稍候再试！");
            e.printStackTrace();
        }
        return ret;
    }





}
